#pragma once
#include <fstream>
#include <iostream>
#include <cmath>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include "../include/common.hpp"
#include "../include/detection.hpp" // Ai模型预测

#define DELAY_NUM1 1000
#define DETECTED_NUM (ROWSIMAGE/4)
#define DELAY_NUM2 500
using namespace std;
using namespace cv;

/**
 * @brief 停车区AI识别与路径规划类
 *
 */
class Parking
{
private:
    /**
     * @brief 场景状态
     *
     */
    enum Step
    {
        init = 0, // 初始化屏蔽
        wait,
        det,      // AI标识检测
        enable,   // 场景使能
        stop      // 准备停车
    };


public:
    Step step = Step::init; // 场景状态
    long preTime;
    long startTime;
    int detected_num=0;
    
    void park_init()
    {
        step=Step::init;
        detected_num=0;
    }

    bool process(vector<PredictResult> predict)
    {
        switch(step)
        {
            case Step::init:
                for(int i=0;i<predict.size();i++)
                {
                    if(predict[i].type==LABEL_CROSSWALK&&(predict[i].y+predict[i].height)>DETECTED_NUM)
                    {
                        detected_num++;
                        if(detected_num>=3)
                        {
                            detected_num=0;
                            preTime = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();//程序开始时间
                            step=Step::wait;
                        }
                    }
                }
            break;
            case Step::wait:
                startTime = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();//帧率计算
                if((startTime-preTime)>DELAY_NUM1)
                {
                    step=Step::det;
                }
            break;
            case Step::det:
                for(int i=0;i<predict.size();i++)
                {
                    if(predict[i].type==LABEL_CROSSWALK&&(predict[i].y)>DETECTED_NUM)
                    {
                        detected_num++;
                        if(detected_num>=3)
                        {
                            detected_num=0;
                            preTime = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();//程序开始时间
                            step=Step::enable;
                        }
                    }
                }
            break;
            case Step::enable:
                startTime = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();//帧率计算
                if((startTime-preTime)>DELAY_NUM2)
                {
                    step=Step::stop;
                }
            break;
            case Step::stop:
                return true;
        }
        return false;
    }


    /**
     * @brief 图像绘制禁行区识别结果
     *
     * @param img 需要叠加显示的图像
     */
    void draw(Mat &img)
    {
        // putText(img, "[5] PARK - ENABLE"+to_string(step), Point(COLSIMAGE / 2 - 30, 10), cv::FONT_HERSHEY_TRIPLEX, 0.3, cv::Scalar(0, 255, 0), 1, CV_AA);
    }
};
